package view;

import linyetao.rbac.domain.dto.UserDto;
import linyetao.rbac.domain.vo.DeptVo;
import linyetao.rbac.domain.vo.Result;
import linyetao.rbac.domain.vo.RoleVo;
import linyetao.rbac.domain.vo.UserVo;
import linyetao.rbac.service.UserService;

public class UserView {
    public static void printMenu(){
        String menu = """
               ---------------------
                       ⻆⾊管理
               ---------------------
                |    0...返回上级    |
                |    1...⽤户列表    |
                |    2...创建⽤户    |
                |    3...更新⽤户    |
                |    4...切换状态    |
               ---------------------
                请输⼊选项：""";
        System.out.print(menu);
    }

    public static UserVo[] cache;

    public static void show(){
        String menuChoice;
        do{
            printMenu();
            menuChoice = MainView.input.nextLine();
            switch (menuChoice){
                case"0":
                    System.out.println("已返回上一级菜单");
                case"1":
                    list();
                    break;
                case"2":
                    create();
                    break;
                case"3":
                    update();
                    break;
                case"4":
                    toggle();
                    break;
                default:
                    System.out.println("输入非法，请输入正确的菜单编号！");
            }
        }while(!"0".equals(menuChoice));
    }

    public static void printError(String message) {
        System.out.println("\033[31m" + message + "\033[0m");
    }

    public static void list() {
        Result result = UserService.list();
        cache = (UserVo[]) result.data;
        System.out.println("ID\t⽤户名\t姓名\t⼿机号\t邮箱\t部⻔名称\t⻆⾊名称\t状态");

        for (UserVo user : cache) {
            System.out.printf("%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
                    user.id, user.username, user.name, user.mobile,
                    user.email, user.deptName, user.roleName,
                    user.enabled ? "启⽤" : "禁⽤");
        }
    }

    public static void create() {
        System.out.print("请输⼊⽤户名：");
        String username = MainView.input.nextLine();
        System.out.print("请输⼊姓名：");
        String name = MainView.input.nextLine();
        System.out.print("请输⼊⼿机号：");
        String mobile = MainView.input.nextLine();
        System.out.print("请输⼊邮箱：");
        String email = MainView.input.nextLine();
        System.out.println("请选择部⻔ ID：");
        int deptId = 0;
        do {
            DeptView.list();
            if (DeptView.cache == null || DeptView.cache.length == 0) {
                printError("系统还没有任何部⻔信息，请先创建部⻔信息。");
                return;
            }
            DeptVo dept = DeptView.getCacheById();
            if (dept != null) {
                deptId = dept.id;
            }
        } while (deptId == 0);
        System.out.println("请选择⻆⾊ ID：");
        int roleId = 0;
        do {
            RoleView.list();
            if (RoleView.cache == null || RoleView.cache.length == 0) {
                printError("系统还没有任何⻆⾊信息，请先创建⻆⾊信息。");
                return;
            }
            RoleVo role = RoleView.getCacheById();
            if (role != null) {
                roleId = role.id;
            }
        } while (roleId == 0);
        UserDto dto = new UserDto(username, name, mobile, email, deptId, roleId);
        Result result = UserService.create(dto);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }

    public static UserVo getCacheById() {
        System.out.print("请输⼊ ID，ID 必须是⼤于 0 的整数：");

        if (!MainView.input.hasNextInt()) {
            MainView.input.nextLine();
            printError("ID 必须为整数，请输⼊整数值");
            return null;
        }

        int id = MainView.input.nextInt();
        MainView.input.nextLine();

        if (id < 1) {
            printError("ID 必须⼤于 0，请重新输⼊");
            return null;
        }

        if (cache == null) {
            Result result = UserService.list();
            cache = (UserVo[]) result.data;
        }

        UserVo user = null;

        for (UserVo item : cache) {
            if (item.id == id) {
                System.out.println("该 ID 对应的数据是：");
                System.out.printf("id:%d，⽤户名：%s，姓名：%s，⼿机号：%s，邮箱：%s，部⻔名称：%s，⻆⾊名称：%s，状态 \n",
                item.id, item.username, item.name, item.mobile,
                        item.email, item.deptName, item.roleName,
                        item.enabled ? "启⽤" : "禁⽤");
                user = item;
                break;
            }
        }
        if (user == null) {
            printError("当前没有该 ID 对应的数据");
            return null;
        }
        return user;
    }

    public static void update() {
        UserVo user = getCacheById();

        if (user == null) {
            return;
        }

        System.out.print("请输⼊⽤户名，直接按回⻋表示不修改：");
        String username = MainView.input.nextLine();
        username = "".equals(username) ? user.username : username;
        System.out.print("请输⼊姓名，直接按回⻋表示不修改：");
        String name = MainView.input.nextLine();
        name = "".equals(name) ? user.name : name;
        System.out.print("请输⼊⼿机号，直接按回⻋表示不修改：");
        String mobile = MainView.input.nextLine();
        mobile = "".equals(mobile) ? user.mobile : mobile;
        System.out.print("请输⼊邮箱，直接按回⻋表示不修改：");
        String email = MainView.input.nextLine();
        email = "".equals(email) ? user.email : email;

        int deptId = 0;
        do {
            DeptView.list();
            DeptVo dept = DeptView.getCacheById();
            if (dept != null) {
                deptId = dept.id;
            }
        } while (deptId == 0);

        int roleId = 0;
        do {
            RoleView.list();
            RoleVo role = RoleView.getCacheById();
            if (role != null) {
                roleId = role.id;
            }
        } while (roleId == 0);

        UserDto dto = new UserDto(username, name, mobile, email, deptId, roleId);
        Result result = UserService.update(dto, user.id);

        if (!result.success) {
            printError(result.message);
            return;
        }

        System.out.println(result.message);
        list();
    }

    public static void toggle() {
        UserVo user = getCacheById();
        if (user == null) {
            return;
        }
        if (user.enabled) {
            System.out.print("输⼊'Y'确认禁⽤，输⼊其它取消禁⽤：");
        } else {
            System.out.print("输⼊'Y'确认启⽤，输⼊其它取消启⽤：");
        }

        String confirm = MainView.input.nextLine();

        if (!"Y".equalsIgnoreCase(confirm)) {
            System.out.println("您已取消修改状态操作");
            return;
        }

        Result result = UserService.toggle(user.id);

        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }
}
